'use strict';

angular.module('Initialize').controller('BadTypeBindController', ['$scope', '$state', 'BaseModule', 'BaseEnum', 'BaseFunction', 'ExtendEnumService', '$location', 'Authentication',
    function($scope, $state, BaseModule, BaseEnum, BaseFunction, ExtendEnumService, $location, Authentication) {
        ExtendEnumService.getExtendEnum('BadType', function(enumData){
            $scope.BadTypeEnum = enumData;
        })
        $scope.btnSave = function(){
            //收集当前质检指标的绑定记录
            if(oldFocusedRecord != null){
                var badTypeBindRecords = $scope.Module.BadTypeBind.CacheRecords;
                for(var i=0; i<badTypeBindRecords.length; i++){
                    if(badTypeBindRecords[i].QCNorm === oldFocusedRecord._id){
                        $scope.Module.BadTypeBind.removeRecord(badTypeBindRecords[i]);
                        i--;
                    }
                }
                $scope.Module.BadTypeBind.Records = $scope.Module.BadTypeBind.CacheRecords.concat($scope.Module._BadTypeBind.CacheRecords);
            }

            for(var i=0; i<$scope.Module.BadTypeBind.Records.length; i++){
                if($scope.Module.BadTypeBind.Records[i].BadType == undefined || 
                    $scope.Module.BadTypeBind.Records[i].BadType == null ||
                    $scope.Module.BadTypeBind.Records[i].BadType < 0){
                    BaseFunction.alert('不良类型不能为空', '保存错误');
                    return;
                }
            }
            $scope.Module.BadTypeBind.save(function(){
                $scope.Module.BadTypeBind.Records = [];
                $scope.Module._BadTypeBind.Records = [];
                $scope.Module._BadTypeBind.emitOnLoadEvent();
                $scope.Module.QCNorm.load();
            });
        };
        $scope.btnCancel = function(){
            $state.reload();
        };
        var oldFocusedRecord = null;
        $scope.rowChange = function(){
            //收集当前质检指标的绑定记录
            if(oldFocusedRecord != null){
                var badTypeBindRecords = $scope.Module.BadTypeBind.CacheRecords;
                for(var i=0; i<badTypeBindRecords.length; i++){
                    if(badTypeBindRecords[i].QCNorm === oldFocusedRecord._id){
                        $scope.Module.BadTypeBind.removeRecord(badTypeBindRecords[i]);
                        i--;
                    }
                }
                $scope.Module.BadTypeBind.Records = $scope.Module.BadTypeBind.CacheRecords.concat($scope.Module._BadTypeBind.CacheRecords);
            }

            oldFocusedRecord = $scope.Module.QCNorm.FocusedRecord;
            $scope.Module._BadTypeBind.Records = [];
            $scope.Module._BadTypeBind.FocusedRecord = null;

            //载入当前质检指标的绑定记录
            if($scope.Module.QCNorm.FocusedRecord.IsLoadBadTypeBind){
                var _BadTypeBindRecords = [];
                var badTypeBindRecords = $scope.Module.BadTypeBind.CacheRecords;

                for(var i=0; i<badTypeBindRecords.length; i++){
                    if(badTypeBindRecords[i].QCNorm === $scope.Module.QCNorm.FocusedRecord._id){
                        _BadTypeBindRecords.push(badTypeBindRecords[i]);
                    }
                }
                $scope.Module._BadTypeBind.Records = _BadTypeBindRecords;  
                $scope.Module._BadTypeBind.emitOnLoadEvent();
            }
            else{
                $scope.Module.BadTypeBind.find({QCNorm: $scope.Module.QCNorm.FocusedRecord._id}, null, $scope.Module.BadTypeBind.Populate, function(records){
                    $scope.Module.QCNorm.FocusedRecord.IsLoadBadTypeBind = true;                    
                    $scope.Module._BadTypeBind.Records = records;
                    $scope.Module.BadTypeBind.Records = $scope.Module.BadTypeBind.CacheRecords.concat(records);
                    $scope.Module._BadTypeBind.emitOnLoadEvent();
                });
            }
        };

        BaseModule.initModule($scope, 'QCNorm');
        $scope.Module.QCNorm.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.QCNorm.PageSize = 15;

        BaseModule.initModule($scope, 'BadTypeBind');
        $scope.Module.BadTypeBind.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.BadTypeBind.PageSize = 15;

        BaseModule.initModule($scope, '_BadTypeBind');
        $scope.Module._BadTypeBind.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module._BadTypeBind.PageSize = 15;
        $scope.Module._BadTypeBind.AfterOnAdd = function(record){
            record.QCNorm = $scope.Module.QCNorm.FocusedRecord._id;
        };
    }
]);
